package com.bjpowernode.mvc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjpowernode.common.enums.RespCode;
import com.bjpowernode.db.domain.ProductInfo;
import com.bjpowernode.db.domain.SysAccount;
import com.bjpowernode.db.result.BidResult;
import com.bjpowernode.mvc.convert.BidConvert;
import com.bjpowernode.mvc.model.result.CommonResult;
import com.bjpowernode.mvc.model.result.Page;
import com.bjpowernode.mvc.convert.ProductConvert;
import com.bjpowernode.mvc.model.dto.CollectProductDTO;
import com.bjpowernode.mvc.model.vo.ProductVO;
import com.bjpowernode.mvc.model.vo.CollectProductVO;
import com.bjpowernode.mvc.service.inter.AccountService;
import com.bjpowernode.mvc.service.inter.BidService;
import com.bjpowernode.mvc.service.inter.ProductService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
public class ProductController {

    private final ProductConvert productConvert;
    private final BidConvert bidConvert;

    private final BidService bidService;
    private final AccountService accountService;
    private final ProductService productService;

    /**
     * 获取产品首页展示数据
     *
     * @return
     */
    @GetMapping("/product/show")
    public CommonResult showProduct() {

        //查询多个类型的产品数据
        CollectProductDTO collectProductDTO = productService.getListProduct();

        //将Dto转换为Vo
        CollectProductVO collectProductVO = productConvert.toCollectProductVO(collectProductDTO);

        return CommonResult.ok(collectProductVO);
    }

    /**
     * 根据产品类型分页查询产品数据
     * 使用父类BaseController对页号和类型进行验证
     *
     * @param type
     * @param pageNo
     * @return
     */
    @GetMapping("/product/list")
    public CommonResult queryByType(
            //产品类型，添加@RequestParam注解保证参数不为空
            @RequestParam(value = "type")
            Integer type,
            //页号，添加@RequestParam注解保证参数不为空
            @RequestParam(value = "pageNo")
            Integer pageNo
    ) {
        //1.检查参数
        if (type == null || type < 0) {
            return CommonResult.warn(RespCode.REQUEST_PARAM_ERR);
        }
        pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;

        //2、调用查询接口查询相应理财产品类型的数据
        IPage<ProductInfo> pageResult = productService.queryPageByType(type, pageNo, 9);

        //3、封装返回的结果数据CommonResult
        //3.1获取分页查询的数据列表  pageResult.getRecords()本次分页记录的数据
        List<ProductVO> productVOList = productConvert.toProductVOList(pageResult.getRecords());
        //当前页,总页数
        Page page = new Page(pageNo, pageResult.getPages());

        //3.2封装数据到map<key,value>
        Map<String, Object> data = new HashMap<>();
        //3.3格式转换
        data.put("productList", productVOList);
        data.put("page", page);
        return CommonResult.ok(data);
    }


    /**
     * 根据产品Id查询产品详情
     *
     * @param productId
     * @return
     */
    @GetMapping("/product/detail")
    public CommonResult queryProductDetail(
            @RequestParam(value = "productId")
            Integer productId,
            @RequestHeader(value = "uid", required = false)
            Integer uid
    ) {
        //默认请求参数错误
        CommonResult commonResult = CommonResult.warn(RespCode.REQUEST_PARAM_ERR);


        if (productId != null && productId > 0) {
            //1、查询产品详情
            ProductInfo productInfo = productService.getProductDetailById(productId);

            //2、查询投资记录
            List<BidResult> bidResultList = bidService.queryProductId(productId, 1, 5);

            //3、查询账户余额   -1表示无效
            BigDecimal accountMoney = new BigDecimal("-1");
            if (uid != null && uid > 0) {
                SysAccount sysAccount = accountService.queryByUserId(uid);
                if (sysAccount != null) {
                    accountMoney = sysAccount.getAvailableMoney();
                }
            }
            //封装数据
            Map<String, Object> data = new HashMap<>();
            data.put("product", productConvert.toProductVo(productInfo));
            data.put("bidList", bidConvert.toListBidVO(bidResultList));
            data.put("accountMoney", accountMoney);

            commonResult = CommonResult.ok(data);
        }
        return commonResult;
    }
}
